<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/10
 * Time: 17:32
 */

namespace craw;
class craw
{
    protected $htmlObj;

    function __construct()
    {
        $this->htmlObj = new simple_html_dom();
    }

    function __destruct()
    {
        $this->htmlObj->clear();
    }

    protected $site = 'http://www.360kan.com';
    protected $tv = 'https://www.360kan.com/dianshi/list.php';
    protected $movie = 'https://www.360kan.com/dianying/list.php';
    protected $zongyi = 'https://www.360kan.com/zongyi/list.php';
    protected $dongman = 'https://www.360kan.com/dongman/list.php';

    /**
     * 获取影视列表
     * @param $_type 1、电视，2、电影，3、综艺，4、动漫
     * @param $page
     * @param $num  获取数量
     * @param $cat
     * @return array
     */
    public function getList($_type, $num, $page = '', $cat = '')
    {
        $_url = '';
        switch ($_type) {
            case 1:
                $_url = $this->tv;
                break;
            case 2:
                $_url = $this->movie;
                break;
            case 3:
                $_url = $this->zongyi;
                break;
            default:
                $_url = $this->dongman;
                break;
        }
        $_url = $_url . "?cat={$cat}&pageno={$page}";

        $this->htmlObj->load($this->http_request($_url));
//        最大分页
        $pagecount = $this->htmlObj->find('#js-ew-page', 0)->last_child()->prev_sibling()->innertext;
        $items = $this->htmlObj->find(".js-tongjic");
        $itemObj = array();
        foreach ($items as $key => $item) {
            if ($key == $num) {
                break;
            }
            $_detailObj = $item->find('.detail', 0);
            $_detail = $_detailObj->outtertext;
            $_title = $_detailObj->find('.s1', 0)->plaintext;
            $_ico = $item->innertext;
            $_url = $item->href;
            $itemObj[] = array("detail" => $_detail, "url" => urlencode(base64_encode($_url)), "ico" => $_ico, "title" => $_title);
        }
        return array('data' => $itemObj, 'pagecount' => $pagecount);
    }

    public function getMovie($url)
    {
        $this->htmlObj->load($this->http_request($this->site . $url));
        //标题
        $movie['title'] = $this->htmlObj->find("h1", 0)->innertext;
        //简介
        $movie['detail'] = str_replace("<a href=\"#\" class=\"js-close btn\">收起&lt;&lt;</a>", "", $this->htmlObj->find(".js-close-wrap", 0)->innertext);

        //播放地址
        foreach ($this->htmlObj->find('.ea-site') as $item) {
            $t['url'] = $item->href;
            $t['title'] = $item->innertext;
            $movie['urls'][] = $t;
        }
        return $movie;
    }

    public function getTv($url)
    {
        $this->htmlObj->load($this->http_request($url));
        //标题
        $tv['title'] = $this->htmlObj->find("h1", 0)->innertext;
        //简介
        $tv['detail'] = str_replace("<a href=\"#\" class=\"js-close btn\">收起&lt;&lt;</a>", "", $this->htmlObj->find(".js-close-wrap", 0)->innertext);

    }

    protected $yhq_site = 'http://www.huizhek.com';
    //今日上新 http://www.huizhek.com/index.php?r=l&cid=0&u=145814
    protected $yhq_today = 'http://www.huizhek.com/index.php?r=l&u=145814';
    //人气榜
    protected $yhq_hot = 'http://www.huizhek.com/index.php?r=p&u=145814';
    //领券直播
    protected $yhq_online = 'http://www.huizhek.com/index.php?r=l&u=145814';

    /**
     * 获取优惠券信息
     * @param $type 类型，1、今日上新，2、人气榜，3、直播
     * @param $num  数量
     * @param string $page 分页
     */
    public function getyhqList($type, $num = -1, $page = '', $cid = 0, $_urlEncode = false)
    {
        switch ($type) {
            case 1:
                $_url = $this->yhq_today;
                break;
            case 2:
                $_url = $this->yhq_hot;
                break;
            default:
                $_url = $this->yhq_online;
                break;
        }
        $pagecount = 0;
        $_url = $_url . "?cid={$cid}";
        if (!empty($page)) {
            $_url = $_url . "&page={$page}";
        }

        $this->htmlObj->load($this->http_request($_url));

        $items = $this->htmlObj->find('.g_over');
        $itemobj = array();
        if ($_urlEncode) {
            foreach ($items as $key => $item) {
                if ($key == $num) {
                    break;
                }
                $url = $item->find('.img', 0)->href;
                $img = $item->find('img', 0)->src;
                $title = $item->find('.title', 0)->find('a', 0)->innertext;
                $price = $item->find('.price', 0)->innertext;
                $coupon = $item->find('.coupon', 0)->innertext;
                $oldprice = $item->find('.old-price', 0)->innertext;

                $itemobj[] = array('url' => urlencode(base64_encode($this->yhq_site . $url)), 'img' => $img, 'title' => $title, 'price' => $price, 'coupon' => $coupon, 'oldprice' => $oldprice);
            }
        } else {
            foreach ($items as $key => $item) {
                if ($key == $num) {
                    break;
                }
                $url = $item->find('.img', 0)->href;
                $img = $item->find('img', 0)->src;
                $title = $item->find('.title', 0)->find('a', 0)->innertext;
                $price = $item->find('.price', 0)->innertext;
                $coupon = $item->find('.coupon', 0)->innertext;
                $oldprice = $item->find('.old-price', 0)->innertext;

                $itemobj[] = array('url' => $this->yhq_site . $url, 'img' => $img, 'title' => $title, 'price' => $price, 'coupon' => $coupon, 'oldprice' => $oldprice);
            }
        }

        if (!count($this->htmlObj->find('.next-page', 0)) == 0) {
            $pagecount = $this->htmlObj->find('.next-page', 0)->prev_sibling()->innertext;
        }

        //获取分类
//        if ($num==-1)
//        {
//            $cats=$this->htmlObj->find('.cat-list',0);
//        }
        return array('data' => $itemobj, 'pagecount' => $pagecount);
    }

    public function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $info = curl_exec($curl);
        curl_close($curl);
        return $info;
    }
}
